// pages/home/components/card/index.js
import {
  home
} from '../../../../api/index'
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    item: {
      type: Object,
      value: {}
    },
    isActivity: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    IsLike: false,
    LikeCount: 0
  },
  attached() {
    this.setData({
      IsLike: this.properties.item.IsLike,
      LikeCount: this.properties.item.LikeCount,
      CreatorHeader: this.properties.item.CreatorHeader||this.properties.item.UserHeader
    })
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 图片加载错误的时候触发; 把当前的错误图片替换成默认图片;
    errDetails(e) {
      this.setData({
        CreatorHeader: '../../../../assets/my/user_64.png'
      })
    },
    Ilike() {
      const obj = {
        LikeID: this.properties.item.ID
      }
      home[this.data.IsLike ? 'removelikeformcontent' : 'addlikeformcontent'](obj).then((res) => {
        if (res.StatusCode === 200) {
          const num = this.data.IsLike ? this.data.LikeCount -= 1 : this.data.LikeCount += 1
          this.setData({
            IsLike: !this.data.IsLike,
            LikeCount: num
          })
        }
      }).catch(err => {
        if (err.statusCode === 401) {
          this.triggerEvent('shudLogin');
        }
      })
    },
    goPgae() {
      var data = JSON.stringify({
        ...this.properties.item,
        ...this.data
      });
      console.log(this.properties.isActivity );
      wx.navigateTo({
        url: this.properties.isActivity ? `/pages/home/activityDetail/activityContent/workDetails/index?id=${this.properties.item.ID}&JoinType=${this.properties.item.JoinType||0}` : '/pages/home/particulars/index?data=' + encodeURIComponent(data),
      })
    }
  }
})